#include "DataProcess.h"

short Concat(uchar MSB, uchar LSB)
{
		return (short)((short)((short)MSB << 8) | (short)LSB);
}

short FloatToFixed(float floatValue, Qvals q)
{
	int temp = (int)( (floatValue) * (int)(1<<q) );
	if (temp > 32767)
		temp = 32767;
	else if (temp < -32768)
		temp = -32768;
	return (short)temp;
}

//传感器数据编码
void InsertChecksum(uchar * decodePacket,int Length) // 01：计算末尾校验位
{
	decodePacket[Length - 1] = 0;
	int i;
	for(i = 0;i<Length - 1;i++)
	{
		decodePacket[Length - 1] += decodePacket[i];
	}
}

void RightShiftByteArray(uchar * byteArray,int Length) // 02: 右移
{
	byteArray[Length - 1] >>= 1;
	int i;
	for(i = Length - 2;i >= 0;i--)
	{
		if ( (byteArray[i] & 0x01) == 0x01 )
			byteArray[i+1] |= 0x80;
		byteArray[i] >>=1;
	}
}

void Encoding(uchar * decodePacket,int de_Length, uchar * encodePacket,int en_Length) // 03:编码
{
	//该函数的功能是编码：将decodePacket转换成encodePacket
	uchar shiftRegister[23] = {}; 
	int i;
	for(i = 0;i<de_Length;i++)
	{
		shiftRegister[i] = decodePacket[i];
	}
	for(i = 0; i< en_Length; i++)
	{
		RightShiftByteArray(shiftRegister,en_Length);//将shiftRegister[]数组从右至左，全部右移一位。
		encodePacket[i] = shiftRegister[i];//将移位之后的shiftRegister[]，从左至右，取出放在encodePacket[]中
		shiftRegister[i] = 0;
	}
	encodePacket[22] |= 0x80;
}


void DataIntoDecodepacket(uchar * decode,float data1,float data2,float data3,\
																	 float data4,float data5,float data6,\
																	 float data7,float data8,float data9)
{
	short gx,gy,gz;
	gx = FloatToFixed(data1, Q_pressure);
	gy = FloatToFixed(data2, Q_pressure);
	gz = FloatToFixed(data3, Q_RPM);
	decode[0] = 0;
	decode[1] = 0;
	//Gyx
	decode[2] = (uchar)((gx & 0xFF00) >>8 );//MSB
	decode[3] = (uchar)(gx & 0x00FF);//LSB
	
	decode[4] = (uchar)((gy & 0xFF00) >>8 );
	decode[5] = (uchar)(gy & 0x00FF);
	
	decode[6] = (uchar)((gz & 0xFF00) >>8 );
	decode[7] = (uchar)(gz & 0x00FF);

}

void LeftShiftByteArray(uchar * byteArray,int Length)
{
	byteArray[0] <<= 1;//第一个值左移一位
	for(int i = 1; i < Length; i++)//从第二个值到最后一个值
	{
		if ( (byteArray[i] & 0x80) == 0x80 )//如果该值高位为1
			byteArray[i-1] |= 0x01;//让该值的左边一个值的低位置为1
		byteArray[i] <<=1;//将该值左移一位
	}	
}

void Decoding(uchar * encodePacket,int en_Length,uchar * decodePacket,int de_Length)
{
	//该函数的功能是解码：将encodePacket转换成decodePacket
	uchar shiftRegister[23] = {}; 
	int i;
	for(i = en_Length - 1; i>=0; i--)
	{
		shiftRegister[i] = encodePacket[i];
		LeftShiftByteArray(shiftRegister,en_Length);
	}
	for(i = 0; i< de_Length; i++)
	{
		decodePacket[i] = shiftRegister[i];
	}
	
	//将解码之后的数据转入到实际传感器数据中去
	DecodepacketIntoData(decodePacket);
}

void FixedToFloat(float * floatValue, short fixedValue, Qvals q)
{
	*floatValue = ((float)(fixedValue) / (float)(1 << ((int)q)));
}

void DecodepacketIntoData(uchar * decode)
{
//	pumpStatus = decode[0];
//	FixedToFloat(&sensorData.currentPressure, Concat(decode[2],decode[3]), Q_pressure);
//	FixedToFloat(&sensorData.currentFlow, Concat(decode[4],decode[5]), Q_flow);
//	FixedToFloat(&sensorData.currentPower, Concat(decode[6],decode[7]),Q_power);
//	FixedToFloat(&sensorData.integralFlow, Concat(decode[8],decode[9]), Q_pressure_Int);
//	FixedToFloat(&sensorData.integralTime, Concat(decode[10],decode[11]), Q_flow_Int);
//	FixedToFloat(&sensorData.integralSavedEnergy, Concat(decode[2],decode[3]), Q_savePower_int);
}




